package com.liwy3.account;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * mybatis-plus代码自动生成器
 **/
public class CodeGenerator {

    /**
     * 重新生成代码时, 是否覆盖service和serviceImpl  N:不覆盖 Y: 覆盖
     */
    private final static String isOverLoadService = "Y";

    private final static String url = "jdbc:mysql://mysql.guzift.com:3307/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC";
    private final static String username = "root";
    private final static String password = "Guzi1234";

    private final static String modelPath = "/seata-starter/account-service";
    private final static String entityPackage = "com.liwy3.account.entity";
    private final static String servicePackage = "com.liwy3.account.service";
    private final static String serviceImplPackage = "com.liwy3.account.service.impl";
    private final static String mapperPackage = "com.liwy3.account.mapper";

    public static void main(String[] args) {
        String[] tables = new String[]{
                "sys_user"
        };
        System.out.println("请确认生成表:" + Arrays.asList(tables) + "对应的ORM文件(防止覆盖已生成的文件): Y/N");
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next();
        if (isOverLoadService.equalsIgnoreCase(next)) {
            String[] models = new String[]{"model", "mapper", "xml", "service"};
            for (String model : models) {
                doHeader(model, tables);
            }
        }
    }

    private static void doHeader(String model, String[] tables) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        if ("xml".equals(model)) {
            gc.setOutputDir(projectPath + modelPath + "/src/main/resources/mapper");
        } else {
            gc.setOutputDir(projectPath + modelPath + "/src/main/java");
        }
        gc.setAuthor("Crazy4J");
        gc.setFileOverride(true);
        gc.setOpen(false);
        gc.setEntityName("%sEntity");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        gc.setBaseColumnList(true);
        gc.setBaseResultMap(true);
        mpg.setGlobalConfig(gc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(tables);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityColumnConstant(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setTablePrefix("t_");
        mpg.setStrategy(strategy);

        if ("xml".equals(model)) {
            PackageConfig packageConfig = new PackageConfig();
            packageConfig.setParent("");
            packageConfig.setXml("");
            packageConfig.setMapper(mapperPackage);
            packageConfig.setEntity(entityPackage);
            mpg.setPackageInfo(packageConfig);
        } else {
            PackageConfig packageConfig = new PackageConfig();
            packageConfig.setParent("");
            packageConfig.setEntity(entityPackage);
            packageConfig.setService(servicePackage);
            packageConfig.setServiceImpl(serviceImplPackage);
            packageConfig.setMapper(mapperPackage);
            mpg.setPackageInfo(packageConfig);
        }
        InjectionConfig injectionConfig = new InjectionConfig() {
            //自定义属性注入:abc
            //在.ftl(或者是.vm)模板中，通过${cfg.abc}获取属性
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("baseModelPath", entityPackage);
                map.put("baseModelPath", mapperPackage);
                this.setMap(map);
            }
        };
        mpg.setCfg(injectionConfig);
        // 关闭默认 xml 生成，调整生成 至 根目录
        TemplateConfig tc = new TemplateConfig();
        if ("mapper".equals(model)) {
            tc.setController(null);
            tc.setEntity(null);
            tc.setService(null);
            tc.setServiceImpl(null);
            tc.setXml(null);
        } else if ("xml".equals(model)) {
            tc.setXml("templates/mapper.xml");
            tc.setController(null);
            tc.setEntity(null);
            tc.setService(null);
            tc.setServiceImpl(null);
            tc.setMapper(null);
        } else if ("model".equals(model)) {
            tc.setController(null);
            tc.setService(null);
            tc.setServiceImpl(null);
            tc.setMapper(null);
            tc.setXml(null);
        } else if ("service".equals(model)) {
            tc.setController(null);
            tc.setMapper(null);
            tc.setXml(null);
            tc.setEntity(null);
        } else if ("controller".equals(model)) {
            tc.setMapper(null);
            tc.setXml(null);
            tc.setService(null);
            tc.setServiceImpl(null);
            tc.setEntity(null);
        }
        mpg.setTemplate(tc);
        mpg.execute();
    }


}
